package edu.mse.camel.client.posterboard.ui.treeoutline;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.emf.common.notify.Adapter;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.Notifier;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.RootEditPart;
import org.eclipse.gef.editparts.AbstractTreeEditPart;
import org.eclipse.gef.editpolicies.RootComponentEditPolicy;

import edu.mse.camel.client.posterboard.posterboard.Posterboard;
import edu.mse.camel.client.posterboard.ui.editor.PosterBoardModelManager;

public class PosterboardTreeEditPart extends AbstractTreeEditPart implements Adapter {

	private PosterBoardModelManager pbModelManager;
	
	public PosterboardTreeEditPart(PosterBoardModelManager pbModelManager) {
		super(pbModelManager.getPosterBoard());
		this.pbModelManager = pbModelManager;
	}
	
	public void activate() {
		if (isActive()) {
			return;
		}
		Posterboard pb = (Posterboard) getModel();
		pb.eAdapters().add(this);
		super.activate();
	}
	
	public void deActivate() {
		if (!isActive()) {
			return;
		}
		Posterboard pb = (Posterboard) getModel();
		pb.eAdapters().remove(this);
		super.deactivate();
	}

	@Override
	public Notifier getTarget() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean isAdapterForType(Object type) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void notifyChanged(Notification notification) {
		refresh();
	}

	@Override
	public void setTarget(Notifier newTarget) {
		// TODO Auto-generated method stub
		
	}
	
	protected void createEditPolicies() {
		// If this editpart is the root content of the viewer, then disallow removal
		if (getParent() instanceof RootEditPart) {
			installEditPolicy(EditPolicy.COMPONENT_ROLE, new RootComponentEditPolicy());
		}
	}
	
	@Override
	public List<Object> getModelChildren() {
		List<Object> contained = new ArrayList<Object>();
		if (pbModelManager.getPapyrusModelManager() != null) {
			contained.add(pbModelManager.getPapyrusModelManager().getTopPackageFromUMLResource());
		}
		if (pbModelManager.getWhiteboard() != null) {
			contained.add(pbModelManager.getWhiteboard());
		}
		return contained;
	}
	
}
